//
//  ViewController.m
//  15-NetEaseHomeWork
//
//  Created by 曹曹 on 16/8/3.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "NetEaseCollectionViewCell.h"
#import "NetEaseTitleLayout.h"

#define kCollectionViewReuse @"cellReuse"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:0.5]

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, NetEaseTitltLayoutDelegate>
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NetEaseTitleLayout *layout;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _height = 44;
    _titleArray = [NSArray arrayWithObjects:@"头条", @"精选", @"娱乐", @"体育", @"网易号", @"汽车", @"美女", @"手机", @"哒哒趣闻", @"科技", @"动漫", @"政务", @"时尚", @"直播", @"游戏", @"房产", @"轻松一刻", @"跟帖", @"历史", @"军事", @"大连", @"家居", @"独家", @"健康", @"彩票", @"图片", nil];
    [self createCollectionView];
}

- (void)createCollectionView {
    self.layout = [[NetEaseTitleLayout alloc] init];
    _layout.titleFont = [UIFont systemFontOfSize:30];
    _layout.delegate =self;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), _height) collectionViewLayout:_layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerNib:[UINib nibWithNibName:@"NetEaseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewReuse];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NetEaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = kArc4RandomColor;
    cell.titleLabel.text = _titleArray[indexPath.row];
    cell.titleLabel.font = _layout.titleFont;
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
  widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = _titleArray[indexPath.row];
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _height)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layout.titleFont}
                                       context:nil];
    return newRect.size.width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
